package com.xuequ.answerme.store;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.CardView;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.xuequ.answerme.R;
import com.xuequ.answerme.adapter.FocusRecyclerViewAdapter;
import com.xuequ.answerme.beans.QuestionBean;
import com.xuequ.answerme.beans.UserBean;
import com.xuequ.answerme.details.QuestionDetailActivity;
import com.xuequ.answerme.manager.QuestionManager;
import com.xuequ.answerme.manager.UserManager;

import java.util.ArrayList;
import java.util.List;

import cn.bmob.v3.BmobUser;

/**
 * Created by Shijie on 2015/9/28.
 *
 */
public class FocusViewPagerFragment extends Fragment {

    private CardView cardView;
    private TextView textView;


    private RecyclerView mRecyclerView;
    private FocusRecyclerViewAdapter mFocusAdapter;
    private List<QuestionBean> mData;

    private boolean isRefreshing = false;

    private QuestionManager questionManager;
    private UserManager userManager;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.store_focus_layout, container, false);

        initViews(view);
        firstRefresh();

        return view;
    }

    private void initViews(View view) {

        //new something
        if (questionManager == null) {
            questionManager = new QuestionManager();
        }
        if (userManager == null) {
            userManager = new UserManager();
        }

        textView = (TextView) view.findViewById(R.id.focus_refresh_remind_text);
        cardView = (CardView) view.findViewById(R.id.focus_refresh_remind_card);

        mRecyclerView = (RecyclerView) view.findViewById(R.id.store_focus_recyclerView);
        RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getActivity());
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecyclerView.setHasFixedSize(true);

        mData = new ArrayList<>();
        mFocusAdapter = new FocusRecyclerViewAdapter(mData);
        mRecyclerView.setAdapter(mFocusAdapter);

        mFocusAdapter.setOnRefreshDoneListener(new FocusRecyclerViewAdapter.OnRefreshDoneListener() {
            @Override
            public void onRefreshDone() {
                //after refreshing
                isRefreshing = true;
            }
        });
        mFocusAdapter.setOnItemClickListener(new FocusRecyclerViewAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                //click the question
                Intent intent = new Intent(getActivity(), QuestionDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putSerializable("questionBean", mData.get(position));
                intent.putExtras(bundle);
                startActivity(intent);
            }

            @Override
            public void onItemLongClick(View view, int position) {

            }
        });

    }

    private void firstRefresh() {
        if (!isRefreshing) {
            isRefreshing = true;
            userManager.findFocus(getActivity(), BmobUser.getCurrentUser(getActivity(), UserBean.class), new UserManager.FindFocusListener() {
                @Override
                public void findFocusOnSuccess(List<QuestionBean> questionBeans, int count) {
                    Log.i("FocusViewPagerFrag", "list size " + questionBeans.size());
                    if (questionBeans.size() != 0) {
                        mFocusAdapter.refreshData(questionBeans);
                    } else {
                        isRefreshing = true;
                    }
                }

                @Override
                public void findFocusOnFailure(String failureMsg) {
                    isRefreshing = false;
                }
            });
        } else {
            Log.i("FocusViewPagerFragment", "is refreshing");
        }

    }

    public void callToRefreshData(){

    }


}
